package cn.edu.scau.cmi.chenjiale.comprehension.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.chenjiale.comprehension.entity.Student;

/**
 * 
 * 学生数据操作Dao
 * 
 * @author chen.jiale
 * @date 2019/05/07
 *
 */
public class StudentDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);

	public static final String NAME = "name";

	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
			Session session = getSession();
			session.save(transientInstance);
			commitTransactionAndClose(session);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			Session session = getSession();
			session.delete(persistentInstance);
			commitTransactionAndClose(session);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Student findById(java.lang.Long id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Session session = getSession();
			Student instance = (Student) session.get("cn.edu.scau.cmi.liangzaoqing.hibernate.Student", id);
			commitTransactionAndClose(session);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			Session session = getSession();
			List results = session.createCriteria("cn.edu.scau.cmi.liangzaoqing.hibernate.Student")
					.add(Example.create(instance)).list();
			commitTransactionAndClose(session);
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName + ", value: " + value);
		try {
			Session session = getSession();
			String queryString = "from Student as model where model." + propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			List list = queryObject.list();
			commitTransactionAndClose(session);
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findAll() {
		log.debug("finding all Student instances");
		try {
			Session session = getSession();
			String queryString = "from Student";
			Query queryObject = session.createQuery(queryString);
			List list = queryObject.list();
			commitTransactionAndClose(session);
			return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Session session = getSession();
			Student result = (Student) session.merge(detachedInstance);
			commitTransactionAndClose(session);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			Session session = getSession();
			session.saveOrUpdate(instance);
			commitTransactionAndClose(session);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			Session session = getSession();
			session.buildLockRequest(LockOptions.NONE).lock(instance);
			commitTransactionAndClose(session);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}